﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ERPForms
{
    public partial class JudeteFrm : FrmRezumat
    {
        private Int32 IDTara = -1;
        private String JudetAdaugat = String.Empty;
        public JudeteFrm(ModuleClass mInfo,Int32 _idtara = -1,String _addJud="")
            : base(mInfo)
        {
            InitializeComponent();
            IDTara = _idtara;
            if (IDTara != -1)
                eSRPDataSet.Judete.IDTaraColumn.DefaultValue = IDTara;
            JudetAdaugat = _addJud;
            if(_addJud!=String.Empty)
                eSRPDataSet.Judete.NumeColumn.DefaultValue = JudetAdaugat;
        }

        private void JudeteFrm_Load(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString);
            this.tariTableAdapter.Connection = con;
            this.tariTableAdapter.Fill(this.eSRPDataSet.Tari);

            this.judeteTableAdapter.Connection = con;
            this.judeteTableAdapter.Fill(this.eSRPDataSet.Judete);
            InchideConexiune(con);
            AddButton("Salveaza", global::ERPStudio.Properties.Resources.salvare1, Salvare, ToolStripItemAlignment.Right, true);
            AddButton("Localitati", global::ERPStudio.Properties.Resources.navigate_48, Localitati, ToolStripItemAlignment.Right, false);
            this.listBndSourceMain.Add(judeteBindingSource);
            listTableAdaptor.Add(this.judeteTableAdapter.Adapter);
            if (JudetAdaugat != String.Empty)
            {
                judeteDGV.AllowUserToAddRows = false;
                Adauga(this, null);
                judeteBindingSource.MoveLast();
                //ESRPDataSet.LocalitatiRow dr = ((DataRowView)judeteBindingSource.Current).Row as ESRPDataSet.LocalitatiRow;
                //dr.Nume = _adLocalitate;
            }
            HandleSalvare += Salvare;
        }

        private void Localitati(object sender, EventArgs e)
        {
            if (judeteDGV.SelectedCells.Count == 0)
            {
                MessageBox.Show("Va rugam selectati judetul pentru care afisati localitatile","Atentie",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            LocalitatiFrm frm = new LocalitatiFrm(this.ModuleInfo);
            frm.CodJudet = Convert.ToInt32(judeteDGV["idJudetColumn",judeteDGV.SelectedCells[0].RowIndex].Value);
            frm.MdiParent = this.MdiParent;
            frm.Text = frm.numeMacheta + " - din judetul " + judeteDGV["numeColumn", judeteDGV.SelectedCells[0].RowIndex].Value; 
            frm.Show();
        }
    }
}
